home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Flock 1.0 beta / flock-1.0RC3.en-US.win32.exe / flock / components / flockSearchAPICraigslist.js < prev    next >
Text File  |  2007-10-18  |  10KB  |  245 lines

  1. //
  2. // BEGIN FLOCK GPL
  3. // 
  4. // Copyright Flock Inc. 2005-2007
  5. // http://flock.com
  6. // 
  7. // This file may be used under the terms of of the
  8. // GNU General Public License Version 2 or later (the "GPL"),
  9. // http://www.gnu.org/licenses/gpl.html
  10. // 
  11. // Software distributed under the License is distributed on an "AS IS" basis,
  12. // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. // for the specific language governing rights and limitations under the
  14. // License.
  15. // 
  16. // END FLOCK GPL
  17. //
  18.  
  19. const CRAIGSLIST_SEARCH_CID         = Components.ID('{67a9e610-313f-11db-a98b-0800200c9a66}');
  20.  
  21. const nsISupports                   = Components.interfaces.nsISupports;
  22. const nsIClassInfo                  = Components.interfaces.nsIClassInfo;
  23. const nsIFactory                    = Components.interfaces.nsIFactory;
  24. const nsIProperties                 = Components.interfaces.nsIProperties;
  25. const nsILocalFile                  = Components.interfaces.nsILocalFile;
  26. const nsIFile                       = Components.interfaces.nsIFile;
  27. const nsIIOService                  = Components.interfaces.nsIIOService;
  28. const nsIFileProtocolHandler        = Components.interfaces.nsIFileProtocolHandler;
  29. const nsIRDFRemoteDataSource        = Components.interfaces.nsIRDFRemoteDataSource;
  30. const nsIRDFDataSource              = Components.interfaces.nsIRDFDataSource;
  31. const flockISearchService           = Components.interfaces.flockISearchService;
  32. const nsIXMLHttpRequest             = Components.interfaces.nsIXMLHttpRequest;
  33.  
  34. const CRAIGSLIST_SEARCH_CONTRACTID  = '@flock.com/?flock-search-craigslist;1';
  35. const DIRECTORY_SERVICE_CONTRACTID  = '@mozilla.org/file/directory_service;1';
  36. const LOCAL_FILE_CONTRACTID         = '@mozilla.org/file/local;1';
  37. const PREFERENCES_CONTRACTID        = '@mozilla.org/preferences-service;1';
  38. const IO_SERVICE_CONTRACTID         = '@mozilla.org/network/io-service;1';
  39. const XMLHTTPREQUEST_CONTRACTID     = '@mozilla.org/xmlextras/xmlhttprequest;1'
  40.  
  41. const flockIError = Components.interfaces.flockIError;
  42. const FLOCK_ERROR_CONTRACTID = '@flock.com/error;1'
  43.  
  44. const CRAIGSLIST_SEARCH_API_URL = "http://www.craigslist.org/cgi-bin/search";
  45. //const CRAIGSLIST_API_KEY = "2dd47dbb72b25cc7130a450255af779b";
  46.  
  47. function craigslistSearchService() {
  48.   var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
  49.   var bundle = sbs.createBundle("chrome://flock/locale/search/search.properties");
  50.   this.serviceName = bundle.GetStringFromName("flock.search.api.craigslist");
  51. }
  52.  
  53. craigslistSearchService.prototype.shortName = "craigslist";
  54. craigslistSearchService.prototype.icon = "http://craigslist.com/favicon.ico";
  55. craigslistSearchService.prototype.ref = "urn:flock:search:craigslist";
  56. craigslistSearchService.prototype.fullResultsUrl = "http://craigslist.org/search/sss?query=";
  57.  
  58. craigslistSearchService.prototype.search = function (aQuery, aNumResults, aListener, aDatasource) {
  59.     var inst = this;
  60.     
  61.     if (!aQuery.length) return;
  62.     var url = CRAIGSLIST_SEARCH_API_URL + "?" 
  63.        + "areaID=1"
  64.        + "&query=" + aQuery
  65.        + "&catAbbreviation=sss"
  66.        + "&format=rss"
  67.  
  68.     debug(url + " < call\n");
  69.     this.req = Components.classes[XMLHTTPREQUEST_CONTRACTID].createInstance(nsIXMLHttpRequest);
  70.     this.req instanceof Components.interfaces.nsIJSXMLHttpRequest;
  71.     this.req.open('GET', url, true);
  72.     var req = this.req;    
  73.     //debug('request is ' + req + '\n');
  74.     this.req.onreadystatechange = function (aEvt) {
  75.       if(inst.req.readyState == 4) {
  76.           // debug("\nRESPONSE\n" + inst.xmlhttp.responseText);
  77.           try {
  78.               if(req.status == 200 || req.status == 201) {
  79.                  try {
  80.                      //processor(listener, inst);
  81.                      var rdf = inst.readXML(req.responseXML, aNumResults, aDatasource);
  82.                      var numResults = inst.getNumResults(req.responseXML);
  83.                      aListener.foundResults(numResults, rdf, inst.shortName, aQuery);
  84.                  } catch(e) {
  85.                      debug(e + " " + e.lineNumber+"\n");
  86.                  }
  87.               }
  88.               else {
  89.                   var faultString = req.responseText;
  90.                   // listener.onFault(faultString);
  91.               }
  92.           } catch(e) {
  93.               debug(e + " " + e.fileName + " " + e.lineNumber + "\n");
  94.               //listener.onError(inst.ERROR_PARSER);
  95.           }
  96.       }
  97.     }
  98.     
  99.     this.req.send(null);  
  100. }
  101.  
  102. craigslistSearchService.prototype.getNumResults = function (xmlDoc) {
  103.     try {
  104.         var results = xmlDoc.getElementsByTagName("item"); 
  105.         return results.length;
  106.     } catch (ex) {
  107.     
  108.     }
  109. }
  110.  
  111. craigslistSearchService.prototype.readXML = function (xmlDoc, aMaxResults, aDatasource) {
  112.   try {                                                     
  113.     var results = xmlDoc.getElementsByTagName("item"); 
  114.  
  115.     // Create an in-memory datasource
  116.     var rdfService = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  117.         .getService(Components.interfaces.nsIRDFService);
  118.     //var rdf = Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"].createInstance(Components.interfaces.nsIRDFDataSource);
  119.     var rdfContainerUtils = Components.classes["@mozilla.org/rdf/container-utils;1"]
  120.         .getService(Components.interfaces.nsIRDFContainerUtils);
  121.         
  122.     var rootNode = rdfService.GetResource("urn:flock:search:craigslist");
  123.     
  124.     // clear any existing results
  125.     var props=aDatasource.ArcLabelsOut(rootNode);
  126.     while(props.hasMoreElements()){
  127.         var prop=props.getNext();
  128.         var target=aDatasource.GetTarget(rootNode,prop,true);
  129.         try {
  130.           //target=target.QueryInterface(Components.interfaces.nsIRDFResource);
  131.           aDatasource.Unassert(rootNode,prop,target)
  132.         }
  133.         catch (e){
  134.             debug ('yahoo error clearing the ds ' + e + '\n');
  135.         }
  136.     }        
  137.         
  138.         
  139.     // Fill it with the results
  140.     rootNode = rdfService.GetResource("urn:flock:search:craigslist");
  141.     var container = rdfContainerUtils.MakeSeq(aDatasource, rootNode);
  142.     
  143.     var maxResults = (results.length < aMaxResults) ? results.length : aMaxResults;
  144.     for (var i = 0; i < maxResults; i++) { 
  145.         var result = results[i];                                                                                                
  146.         var title = result.getElementsByTagName('title')[0].firstChild.nodeValue;
  147.         var clickUrl = result.getElementsByTagName('link')[0].firstChild.nodeValue;
  148.         var url = result.getElementsByTagName('link')[0].firstChild.nodeValue;
  149.         var subject = rdfService.GetResource(url);
  150.  
  151.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#Name");
  152.         var name = rdfService.GetLiteral(title);                
  153.         aDatasource.Assert(subject, predicate, name, true);
  154.  
  155.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#URL");
  156.         var name = rdfService.GetLiteral(url);
  157.         aDatasource.Assert(subject, predicate, name, true);
  158.         
  159.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#favicon");
  160.         var name = rdfService.GetLiteral(this.icon);
  161.         aDatasource.Assert(subject, predicate, name, true);
  162.  
  163.         container.AppendElement(subject);
  164.     }
  165.  
  166.     return aDatasource;
  167.   } catch(ex) {
  168.     debug('craigslistSearchService ' + ex + '\n');
  169.   }
  170. }
  171.  
  172.  
  173. craigslistSearchService.prototype.flags = nsIClassInfo.SINGLETON;
  174. craigslistSearchService.prototype.classDescription = "craigslist Search Service";
  175. craigslistSearchService.prototype.getInterfaces = function (count) {
  176.     var interfaceList = [flockISearchService, nsIClassInfo];
  177.     count.value = interfaceList.length;
  178.     return interfaceList;
  179. }
  180. craigslistSearchService.prototype.getHelperForLanguage = function (count) {return null;}
  181.  
  182. // the nsISupports implementation
  183. craigslistSearchService.prototype.QueryInterface =
  184. function (iid) {
  185.     if (!iid.equals(flockISearchService) && 
  186.         !iid.equals(nsIClassInfo) &&
  187.         !iid.equals(nsISupports))
  188.         throw Components.results.NS_ERROR_NO_INTERFACE;
  189.     if (iid.equals(nsIRDFDataSource) && !this.dataSourceSetup) {
  190.     }
  191.     return this;
  192. }
  193.  
  194. // Module implementation
  195. var FlockSearchModule = new Object();
  196.  
  197. FlockSearchModule.registerSelf =
  198. function (compMgr, fileSpec, location, type)
  199. {
  200.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  201.  
  202.     compMgr.registerFactoryLocation(CRAIGSLIST_SEARCH_CID, 
  203.                                     "craigslist Search JS Component",
  204.                                     CRAIGSLIST_SEARCH_CONTRACTID, 
  205.                                     fileSpec, 
  206.                                     location,
  207.                                     type);
  208.     //necessary category registration
  209.     var catmgr = Components.classes["@mozilla.org/categorymanager;1"]
  210.         .getService (Components.interfaces.nsICategoryManager);
  211.     catmgr.addCategoryEntry('flockISearchService', 'craigslist', CRAIGSLIST_SEARCH_CONTRACTID, true, true);
  212. }
  213.  
  214. FlockSearchModule.getClassObject =
  215. function (compMgr, cid, iid) {
  216.     if (!cid.equals(CRAIGSLIST_SEARCH_CID))
  217.         throw Components.results.NS_ERROR_NO_INTERFACE;
  218.     
  219.     if (!iid.equals(Components.interfaces.nsIFactory))
  220.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  221.     
  222.     return SeachServiceFactory;
  223. }
  224.  
  225. FlockSearchModule.canUnload =
  226. function(compMgr)
  227. {
  228.     return true;
  229. }
  230.     
  231. /* factory object */
  232. var SeachServiceFactory = new Object();
  233.  
  234. SeachServiceFactory.createInstance =
  235. function (outer, iid) {
  236.     if (outer != null)
  237.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  238.     return (new craigslistSearchService()).QueryInterface(iid);
  239. }
  240.  
  241. /* entrypoint */
  242. function NSGetModule(compMgr, fileSpec) {
  243.     return FlockSearchModule;
  244. }
  245.